package com.alan.springbootmybatisxml.controller;


import ch.qos.logback.core.net.SyslogOutputStream;
import com.alan.springbootmybatisxml.entity.ResultVO;
import com.alan.springbootmybatisxml.entity.User;
import com.alan.springbootmybatisxml.enums.UserSexEnum;
import com.alan.springbootmybatisxml.service.UserService;
import com.alan.springbootmybatisxml.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author Alan
 * @Description
 * @date 2020年03月26日 20:59
 */
@RestController
@RequestMapping("rest")
@Slf4j
public class HelloRestController {

    @Value("${com.alan.title}")
    private String title;

    @Autowired
    private UserService userService;

    /**
     * 测试方法
     * @return
     */
    @RequestMapping("hello")
    public ResultVO hello(){
        log.info("rest hello");
        System.out.println("rest hello");
        return ResultUtil.success("hello "+title);
    }


    @RequestMapping("/getUsers")
    public List<User> getUsers() {
        List<User> users=userService.getUsers();
        return users;
    }

    @RequestMapping("/getUser")
    public User getUser(Long id) {
        User user=userService.getUser(id);
        return user;
    }

    @RequestMapping("/add")
    public void save(User user) {
        userService.save(user);
    }

    @RequestMapping("/save")
    public ResultVO save() {
        System.out.println("start save *****");
        userService.save(new User("aa1", "a123456", UserSexEnum.MAN));
        userService.save(new User("bb1", "b123456",UserSexEnum.MAN));
        userService.save(new User("cc1", "c123456",UserSexEnum.MAN));

        return ResultUtil.success("hello "+userService.getUsers().size());
    }

    @RequestMapping(value="update")
    public void update(User user) {
        userService.update(user);
    }

    @RequestMapping(value="/delete/{id}")
    public void delete(@PathVariable("id") Long id) {
        userService.delete(id);
    }
}
